﻿package mortal.game.view.mainUI.roleAvatar
{
    import Message.Game.*;
    import Message.Public.*;
    import com.gengine.core.frame.*;
    import com.greensock.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.utils.*;
    import extend.language.*;
    import fl.events.*;
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.display.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.cd.*;
    import mortal.game.view.common.cd.effect.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.menu.*;
    import mortal.game.view.drugBack.*;
    import mortal.game.view.guide.*;
    import mortal.game.view.mainUI.avatar.*;
    import mortal.game.view.systemSetting.*;
    import mortal.mvc.core.*;

    public class RoleAvatar extends PlayerAvatar
    {
        protected var _freezingLifeEffect:CDDrugFreezingEffect;
        protected var _freezingManaEffect:CDDrugFreezingEffect;
        protected var _energyBar:BaseProgressBar;
        protected var _cdDataLife:ICDData;
        protected var _cdDataMana:ICDData;
        private var _comBat:BitmapNumberText;
        private var _liftPackBtn:GLoadedButton;
        private var _manaPackBtn:GLoadedButton;
        private var _carrerBtn:GImageButton;
        protected var _lifeSlider:GSlider;
        protected var _manaSlider:GSlider;
        private var _lastTime:int = -1;
        private var _tween:TimelineLite;

        public function RoleAvatar()
        {
            this.addLiftBtn();
            this.addManaBtn();
            this.addComBat();
            this.addLifeSlider();
            this.addManaSlider();
            this.addEnergyBar();
            this.refreshDrugNum();
            this.updateEnergy();
            this.showDrugBtn();
            return;
        }// end function

        override protected function addLookPlayerBtn() : void
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            NetDispatcher.addCmdListener(ServerCommand.RoleStaminaUpdate, this.updateEnergy);
            Dispatcher.addEventListener(EventName.RoleShowStaminaFilter, this.showStaminaFilter);
            return;
        }// end function

        protected function onWorldLevelTooltip() : String
        {
            var _loc_1:* = Language.getStringByParam(35377, Cache.instance.publicCache.worldLevel);
            return _loc_1;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            NetDispatcher.removeCmdListener(ServerCommand.RoleStaminaUpdate, this.updateEnergy);
            Dispatcher.removeEventListener(EventName.RoleShowStaminaFilter, this.showStaminaFilter);
            return;
        }// end function

        private function showStaminaFilter(event:DataEvent) : void
        {
            EffectManager.glowFilterReg(this._energyBar, [FilterConst.yellowTipsFilter], 3, 10, 0, 3);
            return;
        }// end function

        public function updateCDEffect(param1:int = 1) : void
        {
            this.unRegisterEffects();
            this._cdDataLife = Cache.instance.cd.getCDData(DrugPackUtil.getCdKeyByType(EDrug._EDrugLifeBag), CDDataType.itemData);
            this._cdDataMana = Cache.instance.cd.getCDData(DrugPackUtil.getCdKeyByType(EDrug._EDrugManaBag), CDDataType.itemData);
            this.registerEffects();
            return;
        }// end function

        protected function unRegisterEffects() : void
        {
            if (this._cdDataLife)
            {
                this.unRegisterLifeFreezing();
            }
            if (this._cdDataMana)
            {
                this.unRegisterManaFreezing();
            }
            return;
        }// end function

        protected function unRegisterLifeFreezing() : void
        {
            if (this._freezingLifeEffect && this._freezingLifeEffect.registed)
            {
                this._freezingLifeEffect.reset();
                this._cdDataLife.removeEffect(this._freezingLifeEffect);
                DisplayUtil.removeMe(this._freezingLifeEffect);
            }
            return;
        }// end function

        protected function unRegisterManaFreezing() : void
        {
            if (this._freezingManaEffect && this._freezingManaEffect.registed)
            {
                this._freezingManaEffect.reset();
                this._cdDataLife.removeEffect(this._freezingManaEffect);
                DisplayUtil.removeMe(this._freezingManaEffect);
            }
            return;
        }// end function

        protected function registerEffects() : void
        {
            if (this._cdDataLife)
            {
                this.registerLifeFreezingEffect();
            }
            if (this._cdDataMana)
            {
                this.registerManaFreezingEffect();
            }
            return;
        }// end function

        protected function registerLifeFreezingEffect() : void
        {
            if (this._cdDataLife == null)
            {
                return;
            }
            if (this._freezingLifeEffect == null)
            {
                this._freezingLifeEffect = new CDDrugFreezingEffect();
                this._freezingLifeEffect.x = this._liftPackBtn.x + 7;
                this._freezingLifeEffect.y = this._liftPackBtn.y + 12;
                this._freezingLifeEffect.setSize(this._liftPackBtn.width - 3, this._liftPackBtn.height - 4);
            }
            if (this._freezingLifeEffect.parent == null)
            {
                this.addChild(this._freezingLifeEffect);
            }
            if (!this._freezingLifeEffect.registed)
            {
                this._cdDataLife.addEffect(this._freezingLifeEffect);
            }
            return;
        }// end function

        protected function registerManaFreezingEffect() : void
        {
            if (this._cdDataMana == null)
            {
                return;
            }
            if (this._freezingManaEffect == null)
            {
                this._freezingManaEffect = new CDDrugFreezingEffect();
                this._freezingManaEffect.x = this._manaPackBtn.x + 6;
                this._freezingManaEffect.y = this._manaPackBtn.y + 12;
                this._freezingManaEffect.setSize(this._manaPackBtn.width - 3, this._manaPackBtn.height - 4);
            }
            if (this._freezingManaEffect.parent == null)
            {
                this.addChild(this._freezingManaEffect);
            }
            if (!this._freezingManaEffect.registed)
            {
                this._cdDataMana.addEffect(this._freezingManaEffect);
            }
            return;
        }// end function

        protected function addLiftBtn() : void
        {
            this._liftPackBtn = UIFactory.gLoadedButton(ImagesConst.LiftPack_upSkin, 260, 55, 14, 26, null, false);
            this._liftPackBtn.toolTipData = this.getLiftPackToolTip;
            this._liftPackBtn.addEventListener(MouseEvent.CLICK, this.showDrugWin);
            return;
        }// end function

        protected function addManaBtn() : void
        {
            this._manaPackBtn = UIFactory.gLoadedButton(ImagesConst.ManaPack_upSkin, 273, 55, 14, 26, null, false);
            this._manaPackBtn.toolTipData = this.getManaPackToolTip;
            this._manaPackBtn.addEventListener(MouseEvent.CLICK, this.showDrugWin);
            return;
        }// end function

        private function showDrugBtn() : void
        {
            addChild(this._liftPackBtn);
            addChild(this._manaPackBtn);
            this.updateCDEffect();
            return;
        }// end function

        override protected function updateLookPlayerBtn() : void
        {
            return;
        }// end function

        override public function updateName(event:Event = null) : void
        {
            if (!_entityInfo)
            {
                return;
            }
            _name.text = _entityInfo.entityInfo.name;
            return;
        }// end function

        override public function updateLife(event:Event = null) : void
        {
            super.updateLife(event);
            if (this._liftPackBtn.parent)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.LifeDrugUse, null));
            }
            return;
        }// end function

        override public function updateMana(event:Event = null) : void
        {
            super.updateMana(event);
            if (this._manaPackBtn.parent)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ManaDrugUse, null));
            }
            return;
        }// end function

        public function setClientValue() : void
        {
            this._lifeSlider.value = ClientSetting.local.drugPackSetting[EDrug._EDrugLifeBag] * this._lifeSlider.maximum / 100;
            this._manaSlider.value = ClientSetting.local.drugPackSetting[EDrug._EDrugManaBag] * this._manaSlider.maximum / 100;
            return;
        }// end function

        public function refreshDrugNum() : void
        {
            if (this._liftPackBtn.parent && this._manaPackBtn.parent)
            {
                this._liftPackBtn.filters = Cache.instance.DrugPack.liftData.leftNum != 0 ? ([]) : ([FilterConst.colorFilter]);
                this._manaPackBtn.filters = Cache.instance.DrugPack.manaData.leftNum != 0 ? ([]) : ([FilterConst.colorFilter]);
            }
            return;
        }// end function

        private function onLifeSetChangeHandler(event:SliderEvent = null) : void
        {
            ClientSetting.local.setDrugPack(Math.round(this._lifeSlider.value / this._lifeSlider.maximum * 100), EDrug._EDrugLifeBag);
            Cache.instance.DrugPack.liftData.autoNum = this._lifeSlider.value / this._lifeSlider.maximum;
            Dispatcher.dispatchEvent(new DataEvent(EventName.LifeDrugUse, null));
            return;
        }// end function

        private function onManaSetChangeHandler(event:SliderEvent = null) : void
        {
            ClientSetting.local.setDrugPack(Math.round(this._manaSlider.value / this._manaSlider.maximum * 100), EDrug._EDrugManaBag);
            Cache.instance.DrugPack.manaData.autoNum = this._manaSlider.value / this._manaSlider.maximum;
            Dispatcher.dispatchEvent(new DataEvent(EventName.ManaDrugUse, null));
            return;
        }// end function

        private function showDrugWin(event:MouseEvent) : void
        {
            var _loc_2:int = 0;
            if (event.target == this._liftPackBtn)
            {
                _loc_2 = 0;
            }
            else if (event.target == this._manaPackBtn)
            {
                _loc_2 = 1;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.OpenDrugWin, _loc_2));
            return;
        }// end function

        protected function addComBat() : void
        {
            this._comBat = UIFactory.bitmapNumberText(168, 8, "PetGrowYellowNum.png", 18, 21, -2, this, 9);
            return;
        }// end function

        override protected function addBg() : void
        {
            this.pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.AvatarBg, 12, 15, this));
            return;
        }// end function

        override protected function addAvatar() : void
        {
            _avatar = UIFactory.gImageBitmap("", 10, 2, _bgSprite);
            _bgSprite.toolTipData = Language.getString(35004);
            return;
        }// end function

        override protected function addCarrer() : void
        {
            this._carrerBtn = UIFactory.gImageButton("", 0, 36, 29, 46, this);
            this._carrerBtn.label = "";
            return;
        }// end function

        override protected function addBuff() : void
        {
            _buffPanel = UICompomentPool.getUICompoment(BuffPanel);
            _buffPanel.createDisposedChildren();
            _buffPanel.x = 115;
            _buffPanel.y = 105;
            this.addChild(_buffPanel);
            return;
        }// end function

        protected function addEnergyBar() : void
        {
            this._energyBar = UICompomentPool.getUICompoment(BaseProgressBar);
            this._energyBar.setProgress(ImagesConst.AvatarEnergy, false, 2, 1, 106, 6);
            this._energyBar.setBg(ImagesConst.AvatarEnergyBg, false, 106, 6);
            this._energyBar.isCacheBmp = true;
            this._energyBar.x = 101;
            this._energyBar.y = 77;
            this._energyBar.mouseChildren = false;
            this._energyBar.mouseEnabled = true;
            this._energyBar.toolTipData = this.getEnergyTooltip;
            this.addChild(this._energyBar);
            return;
        }// end function

        private function getEnergyTooltip() : String
        {
            return Language.getString(35378) + this._energyBar.value * 0.01 + "/" + this._energyBar.totalValue * 0.01 + Language.getString(35379);
        }// end function

        public function updateEnergy(param1:Object = null) : void
        {
            var _loc_2:* = ConstConfig.instance.getValueByName("TConstStaminaMax");
            var _loc_3:* = Cache.instance.role.roleInfo.stamina;
            var _loc_4:* = Cache.instance.buff.getBuffTypeValue(EBuffType._EBuffTypeIncOrDeStaminaMax);
            this._energyBar.setValue(_loc_3, _loc_2 + _loc_4);
            return;
        }// end function

        private function changeLifeSliderPos(event:MouseEvent) : void
        {
            this._lifeSlider.value = _shengmingBar.mouseX - 5;
            this.onLifeSetChangeHandler();
            return;
        }// end function

        override protected function addLifeBar() : void
        {
            _shengmingBar = UICompomentPool.getUICompoment(BaseProgressBar);
            _shengmingBar.setProgress(ImagesConst.AvatarXueBar, false, 2, 1, 156, 11);
            _shengmingBar.setBg(BitmapDataConst.AlphaBMD, true, 156, 11);
            _shengmingBar.setLabel(BaseProgressBar.ProgressBarTextOver, -20, -5);
            _shengmingBar.isCacheBmp = true;
            _shengmingBar.x = 100;
            _shengmingBar.y = 53;
            _shengmingBar.mouseChildren = false;
            _shengmingBar.mouseEnabled = true;
            _shengmingBar.toolTipData = this.getLiftUseDrugToolTip;
            this.addChild(_shengmingBar);
            _shengmingBar.addEventListener(MouseEvent.CLICK, this.changeLifeSliderPos);
            return;
        }// end function

        override protected function addMana() : void
        {
            _mofaBar = UICompomentPool.getUICompoment(BaseProgressBar);
            _mofaBar.createDisposedChildren();
            _mofaBar.setProgress(ImagesConst.AvatarLanBar, false, 2, 1, 132, 11);
            _mofaBar.setBg(BitmapDataConst.AlphaBMD, true, 132, 11);
            _mofaBar.setLabel(BaseProgressBar.ProgressBarTextOver, -20, -4);
            _mofaBar.isCacheBmp = true;
            _mofaBar.x = 100;
            _mofaBar.y = 65;
            _mofaBar.mouseChildren = false;
            _mofaBar.mouseEnabled = true;
            _mofaBar.toolTipData = this.getManaUseDrugToolTip;
            this.addChild(_mofaBar);
            _mofaBar.addEventListener(MouseEvent.CLICK, this.changeManaSliderPos);
            return;
        }// end function

        protected function addLifeSlider() : void
        {
            this._lifeSlider = new GSlider();
            this._lifeSlider.x = 102;
            this._lifeSlider.y = 53;
            this._lifeSlider.styleName = "SliderBtn";
            this._lifeSlider.width = 149;
            this._lifeSlider.height = 13;
            this._lifeSlider.buttonMode = true;
            this._lifeSlider.maximum = 149;
            this._lifeSlider.minimum = 0;
            this._lifeSlider.snapInterval = 1;
            this._lifeSlider.liveDragging = true;
            this._lifeSlider.mouseEnabled = true;
            this._lifeSlider.toolTipData = this.getLiftUseDrugToolTip;
            addChild(this._lifeSlider);
            this._lifeSlider.configEventListener(SliderEvent.THUMB_RELEASE, this.onLifeSetChangeHandler);
            return;
        }// end function

        protected function addManaSlider() : void
        {
            this._manaSlider = new GSlider();
            this._manaSlider.x = 102;
            this._manaSlider.y = 65;
            this._manaSlider.styleName = "SliderBtn";
            this._manaSlider.width = 125;
            this._manaSlider.height = 13;
            this._manaSlider.buttonMode = true;
            this._manaSlider.maximum = 125;
            this._manaSlider.minimum = 0;
            this._manaSlider.snapInterval = 1;
            this._manaSlider.liveDragging = true;
            this._manaSlider.mouseEnabled = true;
            this._manaSlider.toolTipData = this.getManaUseDrugToolTip;
            addChild(this._manaSlider);
            this._manaSlider.configEventListener(SliderEvent.THUMB_RELEASE, this.onManaSetChangeHandler);
            return;
        }// end function

        private function changeManaSliderPos(event:MouseEvent) : void
        {
            this._manaSlider.value = _mofaBar.mouseX - 5;
            this.onManaSetChangeHandler();
            return;
        }// end function

        override protected function addName() : void
        {
            var _loc_1:* = GlobalStyle.textFormatBai;
            _loc_1.size = 12;
            _loc_1.align = TextFormatAlign.LEFT;
            _name = UIFactory.gTextField("", 110, 30, 100, 20, this, _loc_1, false);
            return;
        }// end function

        override protected function addLevel() : void
        {
            var _loc_1:* = GlobalStyle.textFormatHui;
            _loc_1.size = 12;
            _loc_1.color = GlobalStyle.colorHuangUint;
            _loc_1.align = TextFormatAlign.CENTER;
            _level = UIFactory.gTextField("", 19, 82, 25, 25, this, _loc_1, false);
            _level.toolTipData = this.onWorldLevelTooltip;
            return;
        }// end function

        override public function updateLevel(event:Event = null) : void
        {
            var _loc_3:int = 0;
            var _loc_4:ItemData = null;
            var _loc_5:GuideUseItemData = null;
            var _loc_6:SDayFirstRechargeRewardInfo = null;
            if (!_entityInfo)
            {
                return;
            }
            var _loc_2:* = _entityInfo.entityInfo.level;
            _level.text = _loc_2.toString();
            if (_loc_2 == 49 || _loc_2 == 59)
            {
                switch(_loc_2)
                {
                    case 49:
                    {
                        _loc_3 = 110141000;
                        break;
                    }
                    case 59:
                    {
                        _loc_3 = 110141001;
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
                _loc_4 = Cache.instance.pack.backPackCache.getItemByCode(_loc_3);
                if (_loc_4)
                {
                    _loc_5 = new GuideUseItemData();
                    _loc_5.itemData = _loc_4;
                    _loc_5.isGuideToUseItem = true;
                    _loc_5.isCountdown = false;
                    _loc_5.showTxt = Language.getStringByParam(35380, _loc_2) + ItemsUtil.getItemName(_loc_4, "[{0}]");
                    Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideShowBackPackItem, _loc_5));
                }
            }
            if (ClockManager.instance.serverOpenDateNum <= 7)
            {
                _loc_6 = Cache.instance.firstRecharge.firstRechargeRewardInfo;
                if (_loc_6 && _loc_6.rechargeNum == 0)
                {
                    if (_loc_2 == 30 || _loc_2 == 40 || _loc_2 == 50 || _loc_2 == 58 || _loc_2 == 60)
                    {
                        if (!GameController.firstRechargeController.isViewShow)
                        {
                            Dispatcher.dispatchEvent(new DataEvent(EventName.FirstRechargeWinOpen, null));
                        }
                    }
                }
            }
            return;
        }// end function

        override public function updateCarrer() : void
        {
            if (!_entityInfo)
            {
                return;
            }
            var _loc_1:* = _entityInfo.entityInfo.career;
            var _loc_2:* = GlobalClass.getBitmap(GameDefConfig.instance.getECarrerPic(_loc_1));
            this._carrerBtn.setStyle("upSkin", _loc_2);
            this._carrerBtn.setStyle("overSkin", _loc_2);
            this._carrerBtn.setStyle("downSkin", _loc_2);
            this._carrerBtn.setStyle("disabledSkin", _loc_2);
            this._carrerBtn.toolTipData = GameDefConfig.instance.getCarrer(_loc_1);
            return;
        }// end function

        override public function updateAvatar() : void
        {
            if (!_entityInfo)
            {
                return;
            }
            _avatar.imgUrl = AvatarUtil.getPlayerAvatarUrl(_entityInfo.entityInfo.career, _entityInfo.entityInfo.sex, AvatarUtil.Big);
            return;
        }// end function

        public function updateComBat(event:Event = null) : void
        {
            if (!_entityInfo)
            {
                return;
            }
            var _loc_2:* = FrameManager.flashFrame.getTime();
            if (this._lastTime != -1 && _loc_2 - this._lastTime >= 1200)
            {
                this.showFlyAddCombatEffect();
                this._lastTime = _loc_2;
            }
            else
            {
                this._comBat.text = _entityInfo.entityInfo.combat.toString();
                this._lastTime = _loc_2;
            }
            return;
        }// end function

        private function showFlyAddCombatEffect() : void
        {
            this._comBat.text = _entityInfo.entityInfo.combat.toString();
            return;
        }// end function

        private function showFlyAddCombatEffectEnd() : void
        {
            this._comBat.text = _entityInfo.entityInfo.combat.toString();
            return;
        }// end function

        private function getLiftPackToolTip() : String
        {
            var _loc_1:* = Language.getString(35360);
            return Language.getStringByParam(30153, _loc_1, _loc_1, _loc_1, DrugPackUtil.getRecoverValueByType(EDrug._EDrugLifeBag), _loc_1, Cache.instance.DrugPack.liftData.leftNum, 12 + Cache.instance.buff.getBuffTypeValue(EBuffType._EBuffTypeIncOrDeCureInterval));
        }// end function

        private function getManaPackToolTip() : String
        {
            var _loc_1:* = Language.getString(35381);
            return Language.getStringByParam(30153, _loc_1, _loc_1, _loc_1, DrugPackUtil.getRecoverValueByType(EDrug._EDrugManaBag), _loc_1, Cache.instance.DrugPack.manaData.leftNum, 12 + Cache.instance.buff.getBuffTypeValue(EBuffType._EBuffTypeIncOrDeManaInterval));
        }// end function

        private function getLiftUseDrugToolTip() : String
        {
            if (this._lifeSlider == null || _shengmingBar == null)
            {
                return "";
            }
            var _loc_1:* = Language.getString(35360);
            return "<font color=\'#1ccc31\'>" + _shengmingBar.value + "/" + _shengmingBar.totalValue + "</font>\n" + Language.getStringByParam(30154, _loc_1, Math.round(this._lifeSlider.value / this._lifeSlider.maximum * 100), _loc_1);
        }// end function

        private function getManaUseDrugToolTip() : String
        {
            if (_mofaBar == null || this._manaSlider == null)
            {
                return "";
            }
            var _loc_1:* = Language.getString(35381);
            return "<font color=\'#1bbee1\'>" + _mofaBar.value + "/" + _mofaBar.totalValue + "</font>\n" + Language.getStringByParam(30154, _loc_1, Math.round(this._manaSlider.value / this._manaSlider.maximum * 100), _loc_1);
        }// end function

        override public function updateEntity(param1:EntityInfo) : void
        {
            super.updateEntity(param1);
            PlayerMenuRegister.UnRegister(_bgSprite);
            PlayerMenuRegister.Register(_bgSprite, param1, PlayerMenuConst.GroupSelfOpMenu);
            return;
        }// end function

        override protected function addEntityEvent() : void
        {
            super.addEntityEvent();
            if (_entityInfo)
            {
                _entityInfo.addEventListener(EntityInfoEventName.UpdateComBat, this.updateComBat);
                _entityInfo.removeEventListener(EntityInfoEventName.UpdateBuffs, updateEntityBuffer);
            }
            NetDispatcher.addCmdListener("buff_" + EBuffType._EBuffTypeIncOrDeStaminaMax, this.updateEnergy);
            return;
        }// end function

        override protected function removeEntityEvent(event:EntityInfoEvent = null) : void
        {
            super.removeEntityEvent();
            if (_entityInfo)
            {
                _entityInfo.removeEventListener(EntityInfoEventName.UpdateComBat, this.updateComBat);
            }
            NetDispatcher.removeCmdListener("buff_" + EBuffType._EBuffTypeIncOrDeStaminaMax, this.updateEnergy);
            return;
        }// end function

    }
}
